from ev import CmdSet
from game.gamesrc.objects.thing import Thing
from game.gamesrc.commands.command import MyCommand
from contrib import lineeditor

class CmdWrite(MyCommand):
    """
    write

    Usage:
      write <obj>

    Write on an object.

    """
    key = "write"
    aliases = "scrivi"
    locks = "cmd:holds()"

    def func(self):
        "implements the command."

        caller = self.caller
        
        if not self.args:
            caller.msg("Su cosa vuoi scrivere?")
            return

        index, string = self.args[0]
        target = None

        try:
            # this will search for a target
            target = self.get_objects_from_string(index, string, caller)[0]
        except IndexError:
            pass

        if not target:
            caller.msg("Su cosa vuoi scrivere?")
            return

        if not hasattr(target, "read_text"):
            caller.msg("Non puoi scriverci sopra!")
            return

        if hasattr(target, "rewritable") and not target.rewritable and target.read_text:
            caller.msg("C'e' gia' scritto qualcosa.")
            return

        # the load/save codes define what the editor shall do when wanting to
        # save the result of the editing. The editor makes self.buffer and
        # self.caller available for this code - self.buffer holds the editable text.

        loadcode = "obj = self.caller.search('#%s')\n" % target.id
        loadcode += "if obj.db.read_text: self.buffer = obj.db.read_text"

        savecode = "obj = self.caller.search('%s')\n" % target.id
        savecode += "obj.db.read_text = self.buffer\n"
        savecode += "self.caller.msg(\"Scrivi su %s.\" % self.key.split(\"/\")[0])\n"
        savecode += "self.caller.location.msg_contents(\"%s scrive qualcosa su %s.\" % "
        savecode += "(self.caller.name, self.key.split(\"/\")[0]), exclude=self.caller, blind=True)"

        editor_key = "%s/%s" % (target.name, self.obj.name)

        # start editor, it will handle things from here.
        lineeditor.LineEditor(caller, loadcode=loadcode, savecode=savecode, key=editor_key)

class CmdRead(MyCommand):
    """
    write

    Usage:
      write <obj>

    Write on an object.

    """
    key = "read"
    aliases = "leggi"
    locks = "cmd:all()"

    def func(self):
        "implements the command."

        caller = self.caller
        
        if not self.args:
            caller.msg("Che cosa vuoi leggere?")
            return

        index, string = self.args[0]
        target = None

        try:
            # this will search for a target
            target = self.get_objects_from_string(index, string, [caller, caller.location])[0]
        except IndexError:
            pass

        if not target:
            caller.msg("Che cosa vuoi leggere?")
            return

        if not hasattr(target.db, "read_text"):
            caller.msg("Non c'e' nulla da leggere su %s." % target.name)
            return

        string = "Leggi %s, c'e' scritto:\n" % target.name
        string += "---------------------------------------------------------------------------------------------------------\n\n"
        string += "%s\n\n---------------------------------------------------------------------------------------------------------" %  target.read_text
        caller.msg(string)

class CmdSetWrite(CmdSet):
    "CmdSet for the write command"
    key = "writeobj_cmdset"
    def at_cmdset_creation(self):
        "called at cmdset creation"
        self.add(CmdWrite())

class CmdSetRead(CmdSet):
    "CmdSet for the write command"
    key = "readobj_cmdset"
    def at_cmdset_creation(self):
        "called at cmdset creation"
        self.add(CmdRead())



class WriteObj(Thing):
    """
    This implements an object that can write, like a pen.
 
    """
    def at_object_creation(self):
        "Called when object is first created."
        super(WriteObj, self).at_object_creation()
        # add commands 
        self.cmdset.add_default(CmdSetWrite, permanent=True)
        self.db.r_desc = "%s e' qui in attesa di scrivere qualcosa." % self.name


class ReadObj(Thing):
    """
    This implements an object that can be written on, like a sheet of paper.

    """
    def at_object_creation(self):
        "Called when object is first created."
        super(ReadObj, self).at_object_creation()
        self.db.read_text = ""
        self.rewritable = False
        # add commands 
        self.cmdset.add_default(CmdSetRead, permanent=True)



